/*
Author:kermit
Email:455196886@qq.com
Company:http://dragontrail.com/
Creattime:2017-1-08
*/

//依赖jquery
var Adventure = function() {
	var  adventure = this;
	//瀑布流初始化
	adventure.MasonryInit = function() {
		var $list = $('.content-item-show .list');
		function loadImage(url) {
		    var img = new Image();
		    //创建一个Image对象，实现图片的预下载
		    img.src = url;
		    if (img.complete) {
		        return img.src;
		    }
		    img.onload = function () {
		    	return img.src;
		    };
		};
		$list.find('img').each(function(index){
		    loadImage($(this).attr('src'));
		})
		$list.imagesLoaded(function(){
			adventure.masonry = new Masonry('.content-item-show .list', {
				itemSelector: '.item',
			});
		});
	}
	//遮盖
	adventure.CoverswiperInit = function(){
		var $coverSwiper = $('.cover-swiper'),
			$nextBtn = $coverSwiper.find('.cover-button-next'),
			$prevBtn = $coverSwiper.find('.cover-button-prev');
		adventure.coverSwiper = new Swiper ($coverSwiper, {
		    nextButton: $nextBtn,
		    prevButton: $prevBtn,
			autoHeight: false
  		})
	}
	//打开遮盖
	adventure.OpenCoverInit = function(){
		$('.adventure-content').on('click','.com-tip-box',function(){
			var $this = $(this),
				type = $this.data('type'),
				swiperBoxCont = '',
				itemData = {
					id:$this.data('id'),
					likedClass:$this.attr('likedClass'),
					disabled:$this.attr('likedClass')=='active'?'disabled':'',
					likecount:$this.data('likecount'),
					address:$this.data('address'),
					atlas:$this.data('atlas'),
					video:$this.data('video'),
					date:$this.data('date'),
					athor:$this.data('athor'),
					title:$this.data('title'),
					detail:$this.data('detail'),
					nationality:$this.data('nationality'),
					partner:$this.data('partner')
				};
			if(type=="pdf"){
				return;
			}else{
				if(type=="video"){
					var autoplay = Common.screenWidth <= 768 ? '' : 'autoplay';
					swiperBoxCont = '<video controls="controls" '+autoplay+' src="'+itemData.video+'"></video>';
				}else if(type=="atlas"){
					var atlas = itemData.atlas.split(',')
						swiperSlides = '';
					for(i in atlas){
						swiperSlides+='<div class="swiper-slide img-link"><img src="'+atlas[i]+'" alt=""></div>';
					}
					swiperBoxCont = '<div class="swiper-container cover-swiper">'+
									    '<div class="swiper-wrapper">'+swiperSlides+'</div>'+
										'<div class="cover-button-prev"><i class="fa fa-angle-left"></i></div>'+
										'<div class="cover-button-next"><i class="fa fa-angle-right"></i></div>'+
									'</div>';
				}
				console.log('itemData',itemData)
				var athorText = type=="recommend" ? '探险家' :'拍摄者',
					address = itemData.address !=='' ? '<p class="address-info"><i class="iconfont icon-address"></i>'+itemData.address+'</p>' : '',
					date = itemData.date !=='' ? '<p class="clear-float">拍摄于：'+itemData.date+'</p>' : '',
					athor = itemData.athor !==''? '<p>'+athorText+'：<span class="text-red">'+itemData.athor+'</span></p>' : '';
					nationality = itemData.nationality !=='' ? '<p>国籍：<span class="text-red">'+itemData.nationality+'</span></p>' : '';
					partner = itemData.partner !=='' ? '<p>同伴：<span class="text-red">'+itemData.partner+'</span></p>' : '';
					footer = '';
				if(!address || !date || !athor || !nationality || !partner){
					footer = '<div class="sidebar-footer">'+address+date+athor+nationality+partner+'</div>';
				}
				console.log('footer',footer)
				var cover_tpl ='<div class="swiper-cover">'+
									'<div class="swiper-cover-inner">'+
										'<div class="swiper-detail">'+
											'<button class="btn btn-close"><i class="iconfont icon-searchclose"></i></button>'+
											'<div class="swiper-box">'+swiperBoxCont+'</div>'+
											'<div class="sidebar">'+
												'<h3 class="sidebar-title">'+itemData.title+'</h3>'+
												'<div class="thumb-box"><button class="btn btn-thumb '+itemData.likedClass+'" data-likeid="'+itemData.id+'" data-role="shownum" '+itemData.disabled+'><i class="iconfont icon-fabulous"></i><span class="thumb-num">'+itemData.likecount+'</span></button></div>'+
												'<div class="sidebar-detail">'+itemData.detail+'</div>'+footer+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>';
				$('html').addClass('un-scroll');
				$('body').append(cover_tpl);
				adventure.CoverswiperInit();
				$('.swiper-cover .btn-close').click(function(event) {
					$(".swiper-cover").remove();
					$('html').removeClass('un-scroll');
				});
                type=="video" && $('.swiper-cover video').get(0).play();
			}
		})
	}
	//状态设置
	adventure.SetState = function(data){
		adventure.isAjax = false;
		adventure.next_cursor = data.next_cursor;
		adventure.isAjaxEnd = data.next_cursor == 0 ? true : false;
	}
	//滚动加载更多
	adventure.ScrollLoadMoreInit = function(){
		
	}
	//导航切换
	adventure.Switch = function(){
		var $nav = $('.switch-nav'),
			$content = $('.switch-content');
		$nav.find('.nav-item').removeClass('nav-item-active');
		$content.find('.content-item').removeClass('content-item-show');
		$nav.find('.nav-item[data-role="'+adventure.switchState+'"]').addClass('nav-item-active');
		$content.find('.content-item[data-id="'+adventure.switchState+'"]').addClass('content-item-show');
	}
	//切换初始化
	adventure.SwitchInit = function(){
		var hash = window.location.hash.split('#')[1];
		console.log('hash',hash)
		adventure.switchState = hash ? hash : 'all';
		adventure.Switch();
	}();
	adventure.Init = function() {
		adventure.MasonryInit();
		adventure.OpenCoverInit();
		$('.switch-nav .nav-item').click(function(){
			var $this = $(this),
				role = $this.data('role');
			adventure.switchState = role;
			adventure.Switch();
			adventure.MasonryInit();
		});
		adventure.ScrollLoadMoreInit();
		console.log('Adventure init');
	}
}
var Adventure = new Adventure();
Adventure.Init();